<template>
  <div class="app-container product-management-wrap">
    <div class="page-header">
      <div class="page-title">
        <span class="title">产品管理</span>
        <!-- <i class="el-icon-question title-icon" @click="titleClick"></i> -->
        <el-input
          v-model="params.search"
          placeholder="产品名称"
          class="search-input"
        >
          <i
            slot="suffix"
            class="el-input__icon el-icon-search"
            @click="btnSearch"
          ></i>
        </el-input>
      </div>
      <div class="header-right">
        <el-button
          type="primary"
          style="background: #204ec4"
          @click="addProduct"
          ><i class="el-icon-plus" style="margin-right: 10px"></i
          >新建产品</el-button
        >
        <el-button style="padding: 0 !important"
          ><svg-icon icon-class="import" class="import"
        /></el-button>
        <el-button style="padding: 0 !important"
          ><svg-icon icon-class="exporter" class="import"
        /></el-button>
        <!-- <el-dropdown trigger="click" class="more">
          <el-button><i class="el-icon-more"></i></el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item><i class="rp rp-import"></i>导入</el-dropdown-item>
            <el-dropdown-item><i class="rp rp-export"></i>导出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown> -->
      </div>
    </div>

    <div class="shadowShell">
      <div class="search-box">
        <el-button
          v-if="showdeleteBtn"
          style="
            height: 32px;
            margin-right: 10px;
            color: #5a607f !important;
            background-color: rgba(243, 244, 249, 0.9) !important;
            border-radius: 4px !important;
            border: 1px solid rgba(243, 244, 249, 0.9);
          "
          @click="deleteRow()"
        >
          <i class="el-icon-delete"></i>
        </el-button>

        <el-select
          v-if="openScreen"
          v-model="value"
          placeholder="请选择"
          style="width: 150px; margin-right: 10px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <!-- <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="全部" name="first"></el-tab-pane>
          <el-tab-pane label="已上架" name="second"></el-tab-pane>
          <el-tab-pane label="变更中" name="third"></el-tab-pane>
          <el-tab-pane label="预下架" name="fourth"></el-tab-pane>
          <el-tab-pane label="预上架" name="fourth"></el-tab-pane>
        </el-tabs> -->
        <div class="tabsBox" style="margin-left: 0">
          <!-- <span class="options-label">显示：</span> -->
          <el-button
            v-for="item in sceneList.filter((res) => res.isSystem != 0)"
            :key="item.sceneId"
            :class="avtiveSceneId == item.id ? 'activeScene' : 'scene'"
            @click="sceneClick(item, 'clearSceneName')"
            >{{ item.name }}</el-button
          >
          <el-dropdown trigger="click" style="margin-left: 10px">
            <el-button
              size="small"
              :type="activeSceneName ? 'activeScene' : 'scene'"
            >
              {{ activeSceneName
              }}<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown" class="commonDropdown">
              <el-dropdown-item
                v-for="(item, index) in tabs.filter((res) => res.isSystem != 1)"
                :key="item.sceneId"
                :class="avtiveSceneId == item.id ? 'activeScene' : 'scene'"
                @click.native="sceneClick(item)"
              >
                {{ item.name }}
              </el-dropdown-item>
              <el-dropdown-item class="last" @click.native="setScene()">
                <i class="el-icon-s-tools"></i>
                <span>场景设置</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>

        <div>
          <table-header-filters
            ref="tableHeaderFilters"
            @changeState="changeFlagState"
            @filter-change="filterChange"
            :addType="addType"
          />
        </div>
      </div>
      <div v-loading="loading" class="clueTable">
        <clueTable
          ref="clueTable"
          @selection-change="handleSelectionChange"
          :headData="columnList"
          @showFilter="showFilter"
          @showDrawer="showDrawerFun"
          :tableData="tableData"
          :total="total"
          @pagination="pagination"
          @star="starProduct"
          :loadding="loadding"
        >
        </clueTable>
      </div>
    </div>

    <!-- 客户info -->
    <productDrawer
      ref="productDrawer"
      @saveSubmit="saveSubmit"
      @updateProductByIds="updateProductByIds"
      @drawerInfo="drawerInfo"
    />
    <!-- 新建客户 -->
    <addDialog
      ref="addDialog"
      :action="createAction"
      @saveSubmit="saveSubmit"
      :addType="addType"
      dialogTitle="新建客户"
    />

    <sceneDialog
      ref="sceneDialog"
      :tabs="tabs"
      :addType="addType"
      @RefreshScene="RefreshScene"
    />
    <!-- 公海 -->
    <seaDialog ref="seaDialog" />
  </div>
</template>
<script>
import productDrawer from "@/components/productManage/productDrawer/index.vue";
import addDialog from "@/components/DialogCustom/addDialog.vue";
import sceneDialog from "@/components/DialogCustom/sceneDialog.vue";
import seaDialog from "@/components/DialogCustom/seaDialog.vue";
import clueTable from "@/components/clueManage/clues/clueTable";
import TableHeaderFilters from "@/components/FieldFilters/TableHeaderFilters";
import {
  queryPageList,
  addProduct,
  deleteByIds,
  starProduct,
  updateProduct,
  updateProductByIds,
  updateProductStatus,
} from "@/api/product/product";
import { queryScene, queryListHead } from "@/api/customer/common";

export default {
  components: {
    productDrawer,
    addDialog,
    clueTable,
    sceneDialog,
    TableHeaderFilters,
    seaDialog,
  },
  mixins: [],
  data() {
    return {
      activeName: "first",
      search: "",
      sceneList: [],
      avtiveSceneId: "",
      activeSceneName: "场景名称",
      openScreen: false,
      tableData: [],
      columnList: [],
      total: 0,
      createAction: {
        type: "save",
        id: "",
        data: {},
      },
      tabs: [],
      avtiveSceneId: "",
      addType: this.$GlobalStatus.tableType.Product,
      showdeleteBtn: false,
      rowList: [],
      loading: false,
      rowIds: [],
      tableSelectionList: [],
      params: {
        label: this.$GlobalStatus.tableType.Product,
        pageNum: 1,
        pageSize: 10,
        sceneId: undefined,
        search: "",
      },
      options: [],
      value: "",
      loadding: true,
    };
  },
  mounted() {
    this.queryListHead();
    this.queryList();
    this.queryScene();
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
      this.activeName = e;
    },
    filterChange(formList) {
      this.params.searchList = formList;
      this.queryList();
    },
    updateProductStatus(parms) {
      updateProductStatus(parms)
        .then((res) => {})
        .catch((error) => {});
    },
    starProduct(row) {
      starProduct(row.id)
        .then((res) => {
          if (res.code == 0) {
            this.$refs.clueTable.toggleStarOk(row);
          }
        })
        .catch((error) => {});
    },
    updateProductByIds(params) {
      updateProductByIds(params)
        .then((res) => {
          if (res.code == 0) {
            this.$refs.clueTable.toggleStarOk(row);
          }
        })
        .catch((error) => {});
    },
    showFilter(row) {
      this.$refs.tableHeaderFilters.showFilterClick(row);
    },
    btnSearch() {
      this.queryList();
    },
    btnSeaDialog() {
      this.$refs.seaDialog.showFun();
    },
    pagination(pageNum) {
      this.params.pageNum = pageNum;
      this.queryList();
    },
    handleSelectionChange(val) {
      this.rowList = val;
      if (val.length > 0) {
        this.showdeleteBtn = true;
      } else {
        this.showdeleteBtn = false;
      }
      this.rowIds = [];
      this.rowList.forEach((element) => {
        this.rowIds.push(element.id);
      });
    },
    drawerInfo(type, id) {
      if (type == "2") {
        this.deleteRow(id);
      }
      if (type == "1") {
        let ids = [];
        ids.push(id);
        this.$refs.seaDialog.showFun(ids);
      }
      if (type == "3" || type == "4") {
        let ids = [];
        ids.push(id);
        let parms = {
          status: type == "3" ? 0 : 1,
          ids: ids,
        };
        this.updateProductStatus(parms);
      }
    },
    //删除row
    deleteRow(id) {
      console.log(this.rowList);
      let title = "确定删除选中数据吗?";
      if (id) {
        title = "确定删除该数据吗?";
      }
      this.$confirm(title, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          let params = {
            ids: this.rowIds,
          };
          let loading = undefined;
          if (id) {
            params.ids = [id];
            loading = this.$loading({
              lock: true,
              text: "Loading",
              spinner: "el-icon-loading",
              background: "rgba(0, 0, 0, 0.7)",
            });
          }
          deleteByIds(params)
            .then((res) => {
              this.loading = false;
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              if (id) {
                params.ids = [id];
                this.$refs.productDrawer.close();
              }
              if (loading) {
                loading.close();
              }
              this.RefreshAddress();
            })
            .catch((error) => {
              this.loading = false;
              if (loading) {
                loading.close();
              }
            });
        })
        .catch(() => {
          this.loading = false;
          //   this.$message({
          //     type: 'info',
          //     message: '已取消删除'
          //   });
        });
    },
    RefreshScene() {
      this.queryScene();
    },
    queryListHead() {
      queryListHead(this.addType)
        .then((res) => {
          this.columnList = res.data;
        })
        .catch((error) => {});
    },
    queryScene() {
      queryScene(this.addType)
        .then((res) => {
          this.sceneList = res.data;
          this.tabs = this.sceneList;
          this.avtiveSceneId = this.tabs[0].id;
          // this.activeSceneName = this.tabs[0].isSystem == 1 ? '' : this.tabs[0].name;
        })
        .catch((error) => {});
    },
    queryList() {
      queryPageList(this.params)
        .then((res) => {
          this.tableData = res.data.records;
          this.total = Number.parseInt(res.data.total);
          this.loadding = false;
          console.log("tableData------", this.tableData);
        })
        .catch((error) => {
          this.loading = false;
        });
    },
    titleClick() {},
    //场景
    setScene() {
      this.$refs.sceneDialog.showFun();
    },
    //
    sceneClick(item) {
      this.params.sceneId = item.id;
      this.queryListHead();
      this.queryList();
      this.avtiveSceneId = item.id;
      // this.activeSceneName = item.isSystem == 1 ? '' : item.name;
      console.log(item.isSystem);
    },
    // 点击客户名称
    showDrawerFun(rowData) {
      this.$refs.productDrawer.show(rowData);
    },
    cellClass({ row, column, rowIndex, columnIndex }) {
      if (column.property === "merchantNum") {
        return "look--underline";
      } else {
        return "";
      }
    },
    // 新建客户
    addProduct() {
      this.$refs.addDialog.showFun();
    },
    saveSubmit(params) {
      if (params.entity.id) {
        updateProduct(params)
          .then((res) => {
            this.$refs.productDrawer.closeAddFun();
            this.queryList();
          })
          .catch((error) => {
            this.$refs.addDialog.closeLoading();
          });
      } else {
        addProduct(params)
          .then((res) => {
            this.$refs.addDialog.closeFun();
            this.queryList();
          })
          .catch((error) => {
            this.$refs.addDialog.closeLoading();
          });
      }
    },
    /**
     * 创建成功
     */
    saveSuccess(data) {
      if (data.type == "product") {
        // this.handleHandle(data) ---刷新列表
        if (data.createContacts) {
          // this.createType = 'contacts'
          this.createAction = {
            type: "relative",
            crmType: "product",
            data: {
              product: data.data,
            },
          };
          this.createShow = true;
        }
      }
    },
    //更新下拉状态
    changeFlagState(val) {
      this.openScreen = val;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/custom-tab-serch.scss";
@import "@/assets/styles/pageStyle.scss";

.product-management-wrap {
  background-color: #f5f6fa;

  // .f14{
  //     font-size: 14px;
  // }
  .page-header {
    .page-title {
      display: flex;
      align-items: center;

      .title-icon {
        color: #42526e;
        cursor: pointer;
        margin-left: 4px;
        font-size: 16px;
      }
    }

    .header-right {
      .more {
        margin-left: 8px;
      }
    }
  }

  .regular-row {
    display: flex;
    flex-direction: row;
    text-align: left;
    align-items: center;
    width: 100%;

    .search-input {
      width: 200px;
    }

    .options-wrap {
      margin-left: 24px;
      display: flex;
      flex: 1;
      overflow-x: auto;
      white-space: nowrap;
      align-items: center;

      .options-label {
        margin-right: 8px;
      }
    }
  }

  .activeScene {
    color: #fff;
    background-color: #344563;
  }

  .senior-scene {
    margin-top: 8px;
    padding: 16px;
    box-shadow: 0 4px 8px -2px rgba(9, 30, 66, 0.25),
      0 0 1px rgba(9, 30, 66, 0.31);

    .senior-scene-title {
      color: #5e6c84;
    }

    .senior-scene-btn-wrap {
      margin-top: 10px;
    }
  }
}
</style>
